// pages/my-pages/editMember/editMember.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: -1,
    member: {},
    voucherList: []
  },

  //点击删除弹窗的确定
  onDelete: function (e) {
    let that = this;
    let id = that.data.id;
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'member/delMember?id=' + id
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        wx.navigateBack({
          detail: 1
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //点击了选择代金券
  checkVoucherButton: function (e) {
    console.log('点击了选择代金券')
    wx.navigateTo({
      url: '/pages/my-pages/select-voucher/index',
    })
  },
  //点击了查看代金券
  checkMemberVoucherButton: function (e) {
    console.log('点击了查看代金券')
    let memberId = this.data.member.id;
    wx.navigateTo({
      url: '/pages/my-pages/member-voucher-list/index?memberId=' + memberId + '&itemId=&checkFlag=false',
    })
  },
  //点击保存
  confirmSaveButton: function (e) {
    let that = this;
    let obj;
    if (-1 == that.data.id) {
      that.setData({
        'member.voucherList': that.data.voucherList
      })
      obj = {
        method: 'POST',
        showLoading: true,
        url: 'member/add',
        data: that.data.member
      }
    } else {
      obj = {
        method: 'POST',
        showLoading: true,
        url: 'member/update',
        data: that.data.member
      }
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        ui.showToast('保存成功')
        wx.navigateBack({
          detail: 1
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //会员号信息变更
  inputMemberNo: function (e) {
    this.setData({
      'member.memberNo': e.detail.value
    })
  },
  //会员名信息变更
  inputMemberName: function (e) {
    this.setData({
      'member.memberName': e.detail.value
    })
  },
  //联系方式信息变更
  inputMemberPhone: function (e) {
    this.setData({
      'member.memberPhone': e.detail.value
    })
  },
  //折扣信息变更
  inputDiscount: function (e) {
    let discount = e.detail.value;
    discount = discount.replace(/\D/g, '')
    if (null != discount && '' != discount && 100 < discount) {
      ui.showToast('折扣率不能大于100');
      discount = discount.slice(0, 2);
    }
    this.setData({
      'member.discount': discount
    })
  },
  inputFirstPreStoreAmount: function (e) {
    let firstPreStoreAmount = e.detail.value;
    firstPreStoreAmount = firstPreStoreAmount.replace(/\D/g, '')
    this.setData({
      'member.firstPreStoreAmount': firstPreStoreAmount
    })
  },
  inputGiftAmount: function (e) {
    let giftPrice = e.detail.value;
    giftPrice = giftPrice.replace(/\D/g, '')
    this.setData({
      'member.giftPrice': giftPrice
    })
  },
  //查询会员信息
  getMemberFunc: function () {
    let that = this;
    let id = that.data.id;
    if (-1 == id || null == id) {
      that.setData({
        member: {}
      })
    } else {
      //获取会员信息
      let obj = {
        method: 'GET',
        showLoading: false,
        url: 'member/getMemberById?id=' + id
      }
      httpUtils.request(obj).then(res => {
        console.log(res.data.message)
        if (res.data.retCode == 0) {
          // console.log('用户信息: ' + res.data.data)
          this.setData({
            member: res.data.data
          })
        } else {
          ui.showToast(res.data.message)
        }
      }).catch(err => {
        ui.showToast('请求失败')
      });
    }
  },
  //点击充值记录跳转
  toTopUpPage: function () {
    wx.navigateTo({
      url: '/pages/my-pages/member-top-up/index?id=' + this.data.id,
    })
  },
  //点击消费记录跳转
  toExpensePage: function () {
    wx.navigateTo({
      url: '/pages/my-pages/member-expense/index?id=' + this.data.id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('onLoad')
    this.setData({
      id: options.id,
      member: {}
    })
    this.getMemberFunc();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log('onShow')

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})